#!/bin/bash
# This file is part of opentaps Smart Energy Applications Suite (SEAS).

# opentaps Smart Energy Applications Suite (SEAS) is free software:
# you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# opentaps Smart Energy Applications Suite (SEAS) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with opentaps Smart Energy Applications Suite (SEAS).
# If not, see <https://www.gnu.org/licenses/>.


USER="$1"
PASSWORD="$2"
EMAIL="$3"
ADMIN="$4"

if [[ "$#" -ne 1 ]]; then
    echo "usage: $0 username"
    exit 0
fi


MANAGE="manage.py"

if [ ! -f ${MANAGE} ]; then
    MANAGE="../${MANAGE}"
fi

python "${MANAGE}" shell  <<EOF
from opentaps_seas.users.models import User
from allauth.account.models import EmailAddress

if len(User.objects.filter(username='${USER}')) == 0:
    print("That username does not exist")
    exit(1)

User.objects.filter(username='${USER}').delete()
EOF

if [ "$?" -eq 0 ]; then
    echo "Deleted user: ${USER}"
else
    echo "See the error above and try again"
    exit 1
fi
